﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;
using EFEntity;
using Iservices;
using System.Threading.Tasks;
using AutoMapper;
using System.Linq;
using Microsoft.EntityFrameworkCore;

namespace Services
{
    public class userServicesDao : IuserServices
    {
        private readonly MyDbContext mydb;
        private readonly IMapper mapper;
        public userServicesDao(MyDbContext mydb, IMapper mapper)
        {
            this.mydb = mydb;
            this.mapper = mapper;
        }
        public async Task<int> Add(user b)
        {
            Euser etest = mapper.Map<Euser>(b);
            mydb.Euse.Add(etest);
            int result = await mydb.SaveChangesAsync();
            return result;
        }

        public async Task<int> Del(short p)
        {
            Euser st = mydb.Euse.Find(p);
            mydb.Euse.Remove(st);
            int result = await mydb.SaveChangesAsync();
            return result;
        }

        public async Task<List<user>> Select()
        {
            List<Euser> list = new List<Euser>();
            await Task.Run(() => {
                list = mydb.Euse.ToList();
            });
            List<user> list2 = mapper.Map<List<user>>(list);
            return list2;
        }

        public async Task<int> Update(user b)
        {
            Euser etest = mapper.Map<Euser>(b);
            mydb.Attach(etest);
            mydb.Entry<Euser>(etest).State = EntityState.Modified;
            return await mydb.SaveChangesAsync();
        }
    }
}
